<?php
/**
 * alltosun.com  PriceController.Class.php
 * ============================================================================
 * 版权所有 (C) 2015 齐齐哈尔大学计算机与控制工程学院，并保留所有权利。
 * ============================================================================
 * $Author: 范志俊 (fanfzj@163.com) $
 * $Date: 2015-10-22 上午12:24:54 $
 */

namespace Admin\Controller;
use Think\Controller;

class LessonController extends AuthController {

    public function index()
    {
        $lesson_list = M('Lesson')->select();
        //select * from lesson
        for ($i = 0; $i < 5; $i++) {
            for ($j = 0; $j < 7; $j++) {
                $lesson_array[$i][$j] = "";
            }
        }
        foreach ($lesson_list as $lesson) {
            $lesson_array[$lesson['row'] - 1][$lesson['colum'] - 1] = $lesson;
        }
        $this->assign('lesson_list', $lesson_array);
        $this->display('lesson_list');
    }

    //增加价值
    public function add()
    {
        $this->display('lesson_add');
    }

    //检测价值是否存在
    public function check_title()
    {
        $title = I('param');
        $result = check_title('lesson', $title);

        if($result){
            $this->ajaxReturn(array('status' => 'n', 'data' => '该应用价值已存在'));
        }else{
            $this->ajaxReturn(array('status' => 'y', 'data' => ''));
        }
    }

    //编辑价值
    public function edit()
    {
        $id = I('id');
        $lesson_info = M('Lesson')->where(array('Id' => $id))->find();
        $this->assign('lesson_info', $lesson_info);
        $this->display('lesson_add');
    }

    //保存数据
    public function save()
    {
        $id = I('Id', 0);
        $colum=I('colum',0);
        $row=I('row',0);
        $data['Id']=$id;
        if((0<$colum)&&($colum<8)&&($row>0)&&($row<6))
        {
            $data['colum']=$colum;
            $data['row']=$row;
            $info = M('Lesson')->where($data)->find();
            $data['name']  = I('name', '');
            if($info){
                //更新操作
                $result = M('Lesson')->where(array('Id' => $id))->save($data);
                if($result || $result === 0){
                    $this->ajaxReturn(array('status' => 'ok', 'info' => '课程修改成功,正在转跳到课程列表'));
                }else if($result === FALSE){
                    $this->ajaxReturn(array('status' => 'error', 'info' => '课程修改失败'));
                }
            }else{
                //入库操作
                $data['add_time'] = date('Y-m-d H:i:s', time());
                $result = M('Lesson')->add($data);
                if($result){
                    $this->ajaxReturn(array('status' => 'ok', 'info' => '课程添加成功,正在转跳到课程列表'));
                }else if($result){
                    $this->ajaxReturn(array('status' => 'error', 'info' => '课程添加失败'));
                }
            }
        }
        else
        {
            $this->ajaxReturn(array('status' => 'error', 'info' => '课程周次或节次不符合标准'));
        }
    }

    public function del()
    {
        $id=I( 'id' );
        $result=M( 'Lesson' )->delete( $id );
        if($result){
            $data[ 'status' ]=1;
            $data[ 'data' ]='课程删除成功';
        }else if($result){
            $data[ 'status' ]=0;
            $data[ 'data' ]='课程删除失败,请刷新重试';
        }
        $this->ajaxReturn( $data );
    }
    public function assess()
    {
        $lesson_list=M("Lesson")->select();
        $data=array();
        $i=0;
        foreach($lesson_list as $lesson)
        {
            $data[$i]['Id']=$lesson['Id'];
            $assess_list=M("Assess")->where(array("lesson_id"=>$lesson['Id']))->getField('grade',true);
            $sum=0;
            foreach($assess_list as $assess)
            {
                $sum+=(int)$assess;
            }
            $data[$i]['name']=$lesson['name'];
            $data[$i]['grade']=$sum;
            ++$i;
        }
        $this->assign('assess_list', $data);
        $this->display('lesson_assess');
    }
}
